//
//  BaseTableViewController.swift
//  Lottery
//
//  Created by 薛永伟 on 2019/1/21.
//  Copyright © 2019年 薛永伟. All rights reserved.
//

import UIKit

/// 如果是从服务器请求信息流的列表，继承此类，空数据的展示与重加载
class BaseFeedTableViewController: UITableViewController {

    /// 正在请求数据
    var isReqiesting = false
    
    var noDataMessage:String {
        return "暂无相关内容"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.emptyDataSetSource = self
        self.tableView.emptyDataSetDelegate = self
        
        
    }
    
    /// 重新请求数据
    func reloadDataRequesting(){
        
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

}

extension BaseFeedTableViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate {
    
    func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        //判断网络情况 返回不同图片
        return YRAPI.Reachability.isReachable == false ? UIImage(named: "list_no_wifi") : UIImage(named: "datasource_empty")
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
                          NSAttributedString.Key.foregroundColor: UIColor.darkGray]
        let nodataText = self.isReqiesting == false ? noDataMessage: "正在加载中"
        return NSAttributedString(string: YRAPI.Reachability.isReachable == false ? "无网络，点击空白处重试" : nodataText, attributes: attributes)
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTap view: UIView) {
        self.reloadDataRequesting()
    }
    
    
}
